package com.pinyougou.manager.controller;

import com.github.pagehelper.PageInfo;
import com.pinyougou.Result;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.sellergoods.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @RequestMapping("/{id}")
    public TbSpecification findById(@PathVariable(value = "id")Long id){
       TbSpecification specification =  specificationService.getOneById(id);
       return specification;

    }
    /**
     * 修改规格并保存
     */
    @RequestMapping("/update")
    public Result modify(@RequestBody TbSpecification specification) {
        try {
            int num = specificationService.update(specification);
            if (num > 0) {
                //保存成功
                return new Result(true, "修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "修改失败");
    }

    /**
     * 保存规格
     * @param specification
     * @return
     */
    @RequestMapping("/save")
    public Result save(@RequestBody TbSpecification specification) {
        try {
            int num = specificationService.add(specification);
            if (num > 0) {
                //保存成功
                return new Result(true, "添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "添加失败");
    }

    @RequestMapping("/list")
    public PageInfo<TbSpecification> findAll(@RequestBody TbSpecification specification,
                                             @RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
                                             @RequestParam(value = "size",required = true,defaultValue = "5")Integer size){
        PageInfo<TbSpecification> specificationPageInfo = specificationService.findAll(specification,page,size);
        return specificationPageInfo;
    }
}
